/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pub.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pub.entity.PubAffect;
import com.thinkgem.jeesite.modules.pub.service.PubAffectService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/pub/pubAffect")
public class PubAffectController extends BaseController {

	@Autowired
	private PubAffectService pubAffectService;
	
	@ModelAttribute
	public PubAffect get(@RequestParam(required=false) String id) {
		PubAffect entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubAffectService.get(id);
		}
		if (entity == null){
			entity = new PubAffect();
		}
		return entity;
	}
	
	@RequiresPermissions("pub:pubAffect:view")
	@RequestMapping(value = {"list", ""})
	public String list(PubAffect pubAffect, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PubAffect> page = pubAffectService.findPage(new Page<PubAffect>(request, response), pubAffect); 
		model.addAttribute("page", page);
		return "modules/pub/pubAffectList";
	}

	@RequiresPermissions("pub:pubAffect:view")
	@RequestMapping(value = "form")
	public String form(PubAffect pubAffect, Model model) {
		model.addAttribute("pubAffect", pubAffect);
		return "modules/pub/pubAffectForm";
	}

	@RequiresPermissions("pub:pubAffect:edit")
	@RequestMapping(value = "save")
	public String save(PubAffect pubAffect, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pubAffect)){
			return form(pubAffect, model);
		}
		pubAffectService.save(pubAffect);
		addMessage(redirectAttributes, "保存文章成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubAffect/?repage";
	}
	
	@RequiresPermissions("pub:pubAffect:edit")
	@RequestMapping(value = "delete")
	public String delete(PubAffect pubAffect, RedirectAttributes redirectAttributes) {
		pubAffectService.delete(pubAffect);
		addMessage(redirectAttributes, "删除文章单成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubAffect/?repage";
	}
	
	@RequestMapping(value="wxlist")
	public String wxlist(PubAffect pubAffect, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Object[]> page = pubAffectService.affectList(); 

		return renderString(response, page);
	}
	@RequestMapping(value="wxget")
	public String wxget(HttpServletRequest request, HttpServletResponse response,String id) {
		PubAffect entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubAffectService.get(id);
		}
		if (entity == null){
			entity = new PubAffect();
		}
		return renderString(response, entity);
	}
	
	@RequestMapping(value="wxsave")
	public String wxsave(HttpServletRequest request, HttpServletResponse response,String title,String authorname,String article,String imgurl,String authorid,String videourl) {
		String id = UUID.randomUUID().toString().replace("-", "");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String publishtime = df.format(new Date());
		int result = pubAffectService.wxsave(id,title,authorname,article,imgurl,authorid,publishtime,videourl);
		
		return renderString(response, result);
	}
	
	

}